<?php
 
// no direct access
defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.application.component.model');

class SyncrModelFetch extends JModel
{
	var $_siteDetails = null;
	var $_data = null;
		
	function __construct() {
		parent::__construct();
		
		if (!$this->_siteDetails) {
			$this->_setSiteId();
		}					
	}
	
	private function _setSiteId($id = null)
	{
		if (JRequest::getInt('siteId', 0) == 0) {
			JError::raiseError(500, JText::_('SYNCR_NO_SITE_SPECIFIED'));
		}
		
		$this->_siteDetails = new JObject();
		if ($id) {
			$this->_siteDetails->id = $id;
		} else {
			$this->_siteDetails->id = JRequest::getVar('siteId', 0);
		}
					
		if (!$this->_getSiteDetails($this->_siteDetails)) {
			JError::raiseError(500, JText::_('SYNCR_COULD_NOT_GET_SITE_DETAILS'));
		}
		
		$this->_data = null;
	}
	
	private function _getSiteDetails($site)
	{
		$db =& JFactory::getDBO();
		
		$query = 'SELECT name'
				.', url'
				.', color'
				.', logo'
				.', apikey'
				.', image'
				.' FROM #__syncr_sites'
				.' WHERE id = '.$site->id
				;
				
		$db->setQuery($query);
		
		if ($details = $db->loadObject()) {
			$site->name = $details->name;
			$site->url = $details->url;
			$site->color = $details->color;
			$site->logo = $details->logo;
			$site->apikey = $details->apikey;
			$site->image = $details->image;
			
			return true;
		}
		
		return false;
	}
	
	private function _fetch($site, $params = null)
	{
		$systemParameters = &JComponentHelper::getParams( 'com_syncr' );
		
		// load evolve_library
		require_once(JPATH_PLUGINS.'/system/evolvelibrary.php');
		
		// new class
		$evl = new evolveLibrary;
		
		
		$url = $site->url;
	
		if (function_exists('curl_init')) {
			return $this->_curl($site, $params);
		} else {
			JError::raiseError(500, JText::sprintf('SYNCR_COULD_NOT_FETCH_SITE_CURL_NOT_SUPPORTED', $params->task));
		}
	}
	
	private function _curl($site, $params) {
		$systemParameters = &JComponentHelper::getParams( 'com_syncr' );
				
		unset($params->_errors);
		
		$url = $site->url.'/?option=com_syncr';
		
		foreach($params as $param=>$value) {
			$fields[] = $param.'='.$value;
		}
			
		$fields_string = implode('&', $fields);
		
		// open connection
		$connection = curl_init();
		curl_setopt($connection, CURLOPT_URL, $url);
		curl_setopt($connection, CURLOPT_POST, count($fields));
		curl_setopt($connection, CURLOPT_POSTFIELDS, $fields_string);
		
		$curlExec = curl_exec($connection);
		
		if (!$curlExec) {
			curl_close($connection);
			JError::raiseError(500, JText::_('SYNCR_COULD_NOT_SEND_CONTENT_TO_SITE'));
		}

		curl_close($connection);
	}
	
	private function _send($site, $params)
	{
		$systemParameters = &JComponentHelper::getParams( 'com_syncr' );
					
		unset($params->_errors);
				
		$url = $site->url.'/?option=com_syncr';
		
		$params->data = json_encode($params->data);
		
		$fields = array(
			'task='.urlEncode($params->task),
			'processId='.urlEncode($params->processId),
			'data='.urlEncode($params->data),
			'categoryId='.urlEncode($params->categoryId),
		);		
		
		$fields_string = implode('&', $fields);
				
		// open connection
		$connection = curl_init();
		curl_setopt($connection, CURLOPT_URL, $url);
		curl_setopt($connection, CURLOPT_POST, count($fields));
		curl_setopt($connection, CURLOPT_POSTFIELDS, $fields_string);
		
		if (!curl_exec($connection)) {
			curl_close($connection);
			JError::raiseError(500, JText::_('SYNCR_COULD_NOT_SEND_CONTENT_TO_SITE'));
		}
		
		curl_close($connection);
	}

	public function getCategories()
	{
		$params = new JObject();
		$params->task = 'categories';
		
		return $this->_fetch($this->_siteDetails, $params);
	}
	
	public function getContent()
	{
		$params = new JObject();
		$params->task = 'content';
		$params->categoryId = JRequest::getInt('categoryId', 0);
		
		return $this->_fetch($this->_siteDetails, $params);
	}
	
	public function requestItem()
	{	
		$params = new JObject();
		$params->task = 'request';
		$params->itemId = JRequest::getInt('itemId', 0);
		$params->processId = JRequest::getInt('processId', 0);	
		
		return $this->_fetch($this->_siteDetails, $params);
	}
	
	public function receiveItem()
	{	
		$params = new JObject();
		$params->task = 'receive';
		$params->processId = JRequest::getInt('processId', 0);
		$params->data = JRequest::getVar('data', '', 'POST', 'string', JREQUEST_ALLOWRAW);
		$params->data = json_decode($params->data);
		
		$params->categoryId = JRequest::getInt('categoryId', 0);
		
		return $this->_send($this->_siteDetails, $params);
	}
		
	public function process()
	{
		$params = new JObject();
		$params->task = 'process';
		$params->processId = JRequest::getVar('processId', 0);
				
		return $this->_fetch($this->_siteDetails, $params);
	}
	
	public function cleanUp()
	{
		$params = new JObject();
		$params->task = 'cleanup';
		$params->processId = JRequest::getVar('processId', 0);
		
		return $this->_fetch($this->_siteDetails, $params);
	}
}